﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;

namespace WMISrvc
{
    public class Installer
    {
        public static void Install(bool install)
        {
            try
            {
                using (AssemblyInstaller inst = new AssemblyInstaller(typeof(WindowsService.Program).Assembly,null)) 
                {
                    IDictionary state = new Hashtable();
                    inst.UseNewContext = true;
                    try
                    {
                        if (install)
                        {
                            inst.Install(state);
                            inst.Commit(state);
                        }
                        else
                        {
                            inst.Uninstall(state);
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            inst.Rollback(state);
                        }
                        catch (Exception ex1)
                        { }
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
